package com.kk.mybatis.service.Impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpStatus;
import com.kk.mybatis.common.handler.CustomException;
import com.kk.mybatis.entity.Label;
import com.kk.mybatis.entity.Material;
import com.kk.mybatis.entity.MaterialLabelRel;
import com.kk.mybatis.entity.request.RelRequest;
import com.kk.mybatis.mapper.LabelMapper;
import com.kk.mybatis.mapper.MaterialLabelRelDynamicSqlSupport;
import com.kk.mybatis.mapper.MaterialLabelRelMapper;
import com.kk.mybatis.mapper.MaterialMapper;
import com.kk.mybatis.service.LabelService;
import com.kk.mybatis.service.MaterialLabelRelService;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.SqlBuilder.select;

/**
 * @description:
 * @author: Kk
 * @create: 2020-11-27 19:39
 **/
@Service
public class MaterialLabelRelServiceImpl implements MaterialLabelRelService {

    @Resource
    private MaterialMapper materialMapper;
    @Resource
    private MaterialLabelRelMapper materialLabelRelMapper;
    @Resource
    private LabelMapper labelMapper;
    MaterialLabelRelDynamicSqlSupport.MaterialLabelRel materialLabelRel=MaterialLabelRelDynamicSqlSupport.materialLabelRel;

    @Override
    public boolean saveMaterialRel(RelRequest relRequest){
        int lId = relRequest.getLId();
        //判断是否存在对应标签
        Label label = labelMapper.selectByPrimaryKey(lId);
        if (label==null){
            throw new CustomException(HttpStatus.HTTP_BAD_REQUEST,"标签不存在");
        }

        //判断素材是否有效
        Material material = relRequest.getMaterial();
        if (material==null){
            throw new CustomException(HttpStatus.HTTP_BAD_REQUEST,"请输入有效素材");
        }

        int mFlag = materialMapper.insert(material);
        if (mFlag==0) {
            //未插入成功
            throw new CustomException(HttpStatus.HTTP_BAD_REQUEST,"素材插入失败");
        }
        MaterialLabelRel rel=new MaterialLabelRel();
        rel.setmId(material.getId());
        rel.setlId(label.getId());
        rel.setCreateTime(LocalDateTime.now());
        rel.setUpdateTime(LocalDateTime.now());
        int relFlag = materialLabelRelMapper.insert(rel);
        if (relFlag==0) {
            //未插入成功
            throw new CustomException(HttpStatus.HTTP_BAD_REQUEST,"关系插入失败");
        }
        return relFlag==1?true:false;
    }

    @Override
    public int saveRel(int mId, int lId) {
        //检验mId和lId是否存在
        Material material = materialMapper.selectByPrimaryKey(mId);
        Assert.notNull(material,"不存在该素材");
        Label label = labelMapper.selectByPrimaryKey(lId);
        Assert.notNull(label,"不存在该标签");
        //插入关系
        SelectStatementProvider statementProvider=select(materialLabelRel.id).from(materialLabelRel)
                .where(materialLabelRel.mId,isEqualTo(mId))
                .and(materialLabelRel.lId,isEqualTo(lId))
                .and(materialLabelRel.status,isEqualTo(0))
                .build()
                .render(RenderingStrategies.MYBATIS3);
        //判断是否重复插入
        int rows=0;
        MaterialLabelRel materialLabelRel = materialLabelRelMapper.selectOne(statementProvider);
        if (materialLabelRel==null){
            MaterialLabelRel rel = new MaterialLabelRel();
            rel.setmId(mId);
            rel.setlId(lId);
            rel.setStatus(0);
            rel.setCreateTime(LocalDateTime.now());
            rel.setUpdateTime(LocalDateTime.now());
            rows = materialLabelRelMapper.insert(rel);
        }
        return rows;
    }
}
